<?php
declare(strict_types=1);
/**
 * MineAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using MineAdmin.
 *
 * @Author X.Mo<root@imoi.cn>
 * @Link   https://gitee.com/xmo/MineAdmin
 */

namespace Plugin\Alarm\Repository;

use Plugin\Alarm\Model\RdAlarmMsg;
use Hyperf\Database\Model\Builder;
use App\Repository\IRepository;
use Hyperf\Collection\Arr;

/**
 * 报警事件回调 Repository类
 */
class RdAlarmMsgRepository extends IRepository
{
   public function __construct(
        protected readonly RdAlarmMsg $model
    ) {}

    /**
     * 搜索处理器
     * @param Builder $query
     * @param array $params
     * @return Builder
     */
    public function handleSearch(Builder $query, array $params): Builder
    {
        
        // 
        if (isset($params['id']) && filled($params['id'])) {
            $query->where('id', '=', $params['id']);
        }

        // 报警类型1:雷达 2:相机
        if (isset($params['type']) && filled($params['type'])) {
            $query->where('type', '=', $params['type']);
        }

        // 报警图片地址
        if (isset($params['alarm_pic_path']) && filled($params['alarm_pic_path'])) {
            $query->where('alarm_pic_path', 'like', '%'.$params['alarm_pic_path'].'%');
        }

        // 报警图片名称
        if (isset($params['alarm_pic_name']) && filled($params['alarm_pic_name'])) {
            $query->where('alarm_pic_name', 'like', '%'.$params['alarm_pic_name'].'%');
        }

        // 摄像头ID
        if (isset($params['camera_id']) && filled($params['camera_id'])) {
            $query->where('camera_id', '=', $params['camera_id']);
        }

        // 摄像头地址
        if (isset($params['camera_url']) && filled($params['camera_url'])) {
            $query->where('camera_url', 'like', '%'.$params['camera_url'].'%');
        }

        // 图片高度
        if (isset($params['image_height']) && filled($params['image_height'])) {
            $query->where('image_height', '=', $params['image_height']);
        }

        // 图片宽度
        if (isset($params['image_width']) && filled($params['image_width'])) {
            $query->where('image_width', '=', $params['image_width']);
        }

        // 检测结果数据
        if (isset($params['result_data']) && filled($params['result_data'])) {
            $query->where('result_data', '=', $params['result_data']);
        }

        // 感兴趣区域
        if (isset($params['roi']) && filled($params['roi'])) {
            $query->where('roi', '=', $params['roi']);
        }

        // 源图片地址
        if (isset($params['src_pic_path']) && filled($params['src_pic_path'])) {
            $query->where('src_pic_path', 'like', '%'.$params['src_pic_path'].'%');
        }

        // 源图片名称
        if (isset($params['src_pic_name']) && filled($params['src_pic_name'])) {
            $query->where('src_pic_name', 'like', '%'.$params['src_pic_name'].'%');
        }

        // 任务ID
        if (isset($params['task_id']) && filled($params['task_id'])) {
            $query->where('task_id', '=', $params['task_id']);
        }

        // 时间戳
        if (isset($params['timestamp']) && filled($params['timestamp'])) {
            $query->where('timestamp', '=', $params['timestamp']);
        }

        // 预警ID
        if (isset($params['yj_data_id']) && filled($params['yj_data_id'])) {
            $query->where('yj_data_id', '=', $params['yj_data_id']);
        }

        // 设备序列号
        if (isset($params['device_serial']) && filled($params['device_serial'])) {
            $query->where('device_serial', '=', $params['device_serial']);
        }

        // 通道号
        if (isset($params['channel_no']) && filled($params['channel_no'])) {
            $query->where('channel_no', '=', $params['channel_no']);
        }

        // 消息类型
        if (isset($params['msg_type']) && filled($params['msg_type'])) {
            $query->where('msg_type', '=', $params['msg_type']);
        }

        // 报警类型
        if (isset($params['alarm_type']) && filled($params['alarm_type'])) {
            $query->where('alarm_type', '=', $params['alarm_type']);
        }

        // 文件地址
        if (isset($params['file_url']) && filled($params['file_url'])) {
            $query->where('file_url', '=', $params['file_url']);
        }

        // 创建时间
        if (isset($params['created_at']) && filled($params['created_at']) && is_array($params['created_at']) && count($params['created_at']) == 2) {
            $query->whereBetween(
                'created_at',
                [ $params['created_at'][0], $params['created_at'][1] ]
            );
        }

        // 更新时间
        if (isset($params['updated_at']) && filled($params['updated_at']) && is_array($params['updated_at']) && count($params['updated_at']) == 2) {
            $query->whereBetween(
                'updated_at',
                [ $params['updated_at'][0], $params['updated_at'][1] ]
            );
        }

        return $query;
    }
}